import 'package:flutter/material.dart';
import 'package:flutter_demo/views/newsList.dart';

class Other extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new OtherState();
  }
}

class OtherState extends State<Other> {
  int _curTab = 0;
  @override
  Widget build(BuildContext context) {
    Map args = ModalRoute.of(context).settings.arguments;
    return new WillPopScope(
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text('other page from ' + args['from']),
        ),
        body: new Column(
          children: <Widget>[
            new Row(
              children: <Widget>[
                new GestureDetector(
                  child: new Container(
                    child: new Text('1', style: new TextStyle(color: _curTab == 0 ? Colors.black : Colors.grey)),
                  ),
                  onTap: () {setState(() {
                  _curTab = 0; 
                  });},
                ),
                new GestureDetector(
                  child: new Container(
                    child: new Text('2', style: new TextStyle(color: _curTab == 1 ? Colors.black : Colors.grey)),
                  ),
                  onTap: () {setState(() {
                  _curTab = 1; 
                  });},
                ),
                new GestureDetector(
                  child: new Container(
                    child: new Text('3', style: new TextStyle(color: _curTab == 2 ? Colors.black : Colors.grey)),
                  ),
                  onTap: () {setState(() {
                  _curTab = 2; 
                  });},
                ),
              ],
            ),
            new Flexible(
              flex: 1,
              child: new Stack(// Stack + Offstage 控制组件的显示跟隐藏，这样tab切换组件不会销毁重绘
                children: <Widget>[
                  new Offstage(
                    offstage: _curTab != 0,
                    child: new NewsList('0'),
                  ),
                  new Offstage(
                    offstage: _curTab != 1,
                    child: new NewsList('1'),
                  ),
                  new Offstage(
                    offstage: _curTab != 2,
                    child: new NewsList('2'),
                  ),
                ],
              ),
            ),
            new Flexible(
              flex: 1,
              child: new Container(
                child: new GestureDetector(
                  child: new Text('go back'),
                  onTap: (){
                    Navigator.of(context).pop('turn back msg1');
                  },
                ),
              ),
            )
          ],
        ),
      ),
      onWillPop: () {// 监听返回键
        Navigator.of(context).pop('turn back msg2');
        return Future.value(false);// 因为已经调用pop方法以便在页面返回时回传参数，所以return false阻止原本的页面出栈行为
      },
    );
  }
}